<?php
include 'wp-blog-header.php';

/* Unique Id for CSS Blocks Avoids other ppl tryin to overide our CSS */
$uniqid = uniqid();
/* Unique Id for CSS Blocks */

/* Decryption & Variable Assignment etc... */
$param = $_GET['p'];
$url_page = $_GET['pg'];
$url_category = $_GET['c'];
$url_city = $_GET['l'];

$decrypted_param = decrypt($param,'1b2u3z4z5r6');
//echo $param;
$decrypted_param = explode('$',$decrypted_param);
//print_r($param);
//exit();

//$params = 'name=deal&type=sale,daily&items=10&format=json';
$params = $decrypted_param[0];

/* Filter Pagination */
if(!empty($url_page)){
$params = $params.'&page='.$url_page;
}
else{
$url_page = 1;
}
/* Filter Pagination */

/* Filter Catgeory */
if(!empty($url_category)){
$params = $params.'&category='.urlencode($url_category);
}
/* Filter Catgeory */

/* Filter City */
if(!empty($url_city)){
$params = $params.'&cities='.urlencode($url_city);
}
/* Filter City */

//$fields = 'title,deal_link,image,store,category,city,map,ads,poweredby,ends';
$fields = $decrypted_param[1];
//$look = 'wrap_width=800px,unit_background=#F3F3F3';
$look = $decrypted_param[2];

//$api = '4d9ae3a93b058';
$api_key = $decrypted_param[3];

//$tp = encrypt($params.'$'.$fields.'$'.$look.'$'.$api,'1b2u3z4z5r6');
//echo 'http://buzzr.in/buzzr_whitelabel.php?p='.$tp;
//exit();

/* Decryption & Variable Assignment etc... */


/* API Key Validation */
if(!empty($api_key))
{
  $api_pod = new Pod('api');
  $api_pod->page = 1;
  $api_pod->search = 1;
  $api_pod->findRecords('id DESC', -1, "t.api_key ='$api_key'");
  $api_exists = $api_pod->getTotalRows();
  if($api_exists > 0)
  {
    while($api_pod->fetchRecord()){
      $client_name = $api_pod->get_field('name');
      $client_name = explode('_',$client_name);
      $client_name = $client_name[0];    
    }
  }
}
/* API Key Validation */

/* If Api Key is Valid Then Proceed */
if($api_exists > 0)
{

/* Whitelabel Clients Params */
$fields = explode(',',$fields);

$look = explode(',',$look);

foreach($look as $vlook){
  $param_look = explode('=',$vlook);
  switch($param_look[0]){
    case 'wrap_width':
      $wrap_width = $param_look[1];
      break;
    case 'unit_background':
      $unit_background = $param_look[1];
      break;
  } 
}

if(empty($wrap_width)){
  $wrap_width = '700px';
}

if(empty($unit_background)){
$unit_background = '#F3F3F3';
}
/* Whitelabel Clients Params */

/* Whitelabel Clients Offers */
function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}
$url = BUZZR_URL.'/api2/?'.$params.'&apikey='.$api_key;
$curl_json_output = download_page($url);
//echo $url;
$json_decoded = json_decode($curl_json_output, true);

//echo "<pre>";
//print_r($json_decoded);
//exit();

/* Whitelabel Clients Offers */
?>
<style>
#whitelabel_wrap_<?php echo $uniqid; ?>{
  width: <?php echo $wrap_width; ?>;
  font-size: 15px;
}

.title_<?php echo $uniqid; ?>{
  font-size: 18px;
}

.unit_wrapper_<?php echo $uniqid; ?>{
 width: 61%;
 float:left;
}

.unit_ads_<?php echo $uniqid; ?>{
  min-height:60px;
  text-align:center;
  -moz-border-radius: 10px 10px 10px 10px;
  -webkit-border-radius: 10px 10px 10px 10px;
  -moz-box-shadow: 2px 2px 4px 0 #AAAAAA;
  background: <?php echo $unit_background; ?>;
  margin-top: 15px;
  padding: 5px;
}

.unit_<?php echo $uniqid; ?>{
  -moz-border-radius: 10px 10px 10px 10px;
  -webkit-border-radius: 10px 10px 10px 10px;
  -moz-box-shadow: 2px 2px 4px 0 #AAAAAA;
  background: <?php echo $unit_background; ?>;
  margin-top: 15px;
  padding: 5px;
  min-height: 110px;
  *height: 100px;
}

.unit_<?php echo $uniqid; ?> img{
  float:left;
  width: 100px;
  height: 100px;
  padding: 5px 10px;
}

a{
  text-decoration: none;
}

.cat_<?php echo $uniqid; ?> a, .cat_list_<?php echo $uniqid; ?> a{
  color: black;
}

.cat_<?php echo $uniqid; ?> a:hover, .cat_list_<?php echo $uniqid; ?> a:hover{
  color: blue;
}

.powered_<?php echo $uniqid; ?>{
  text-align: center;
  margin-top: 20px;
}

.powered_<?php echo $uniqid; ?> img, .unit_<?php echo $uniqid; ?> img{
  border: none;
}

.paginate_<?php echo $uniqid; ?>{
  margin-top:10px;
  font-size:16px;
  text-align: center;
}

.cat_list_<?php echo $uniqid; ?>, .ads_sidebar_<?php echo $uniqid; ?>{
  float:right;
  width:36%;
  margin-top:15px;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  -moz-box-shadow: 2px 2px 4px 0 #AAAAAA;
  background: <?php echo $unit_background; ?>;
  padding: 5px;
  font-size: 18px;
  text-align: center;
}

.no_offer_<?php echo $uniqid; ?>{
color: Maroon;
font-size: 20px;
min-height: 20px;
text-align: center;
}
</style>

<script type="text/javascript">
function cdtime(container, targetdate){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
this.currentTime=new Date()
this.targetdate=new Date(targetdate)
this.timesup=false
this.updateTime()
}

cdtime.prototype.updateTime=function(){
var thisobj=this
this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}

cdtime.prototype.displaycountdown=function(baseunit, functionref){
this.baseunit=baseunit
this.formatresults=functionref
this.showresults()
}

cdtime.prototype.showresults=function(){
var thisobj=this

var timediff=(this.targetdate-this.currentTime)/1000 //difference btw target date and current date, in seconds
if (timediff<0){ //if time is up
this.timesup=true
this.container.innerHTML=this.formatresults()
return
}
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
hourfield=dayfield*24+hourfield
dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
minutefield=dayfield*24*60+hourfield*60+minutefield
dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff
dayfield=hourfield=minutefield="n/a"
}
this.container.innerHTML=this.formatresults(dayfield, hourfield, minutefield, secondfield)
setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}

/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////

//Create your own custom format function to pass into cdtime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left

//The values of these arguments may change depending on the "baseunit" parameter of cdtime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc


function formatresults(){
if (this.timesup==false){//if target date/time not yet met
var displaystring= arguments[0]+" days "+arguments[1]+":"+arguments[2]+":"+arguments[3]
}
else{ //else if target date/time met
var displaystring="This offer has expired"
}
return displaystring
}

function formatresults2(){
if (this.timesup==false){ //if target date/time not yet met
var displaystring="<span class='lcdstyle'><b>Time Left: </b>"+arguments[0]+" days "+arguments[1]+":"+arguments[2]+":"+arguments[3]+"</span>"
}
else{ //else if target date/time met
var displaystring="This offer has expired" //Don't display any text
}
return displaystring
}
</script>

<!-- Whitelabel Content Wrap -->
<div id="whitelabel_wrap_<?php echo $uniqid; ?>">
     <?php if(count($json_decoded) > 0){ ?>
     <!-- Deal Units Wrap -->
     <div class="unit_wrapper_<?php echo $uniqid; ?>">
     <?php 
     /* Deals Loop */
     foreach($json_decoded as $key=>$value){
      
      /* Ads Unit */
      if(in_array('ads',$fields)){
         if(($key == (count($json_decoded) - 2)) || $key == 2){
         echo '<div class="unit_ads_'.$uniqid.'">';
         $ads_unit_width = 0.61 * (str_replace('px','',$wrap_width));
         if($ads_unit_width > 468){
             echo '<script type="text/javascript"><!--
              google_ad_client = "ca-pub-3655588719499040";
              /* Buzzr Whitelabel Network */
              google_ad_slot = "1866554202";
              google_ad_width = 468;
              google_ad_height = 60;
              //-->
             </script>
             <script type="text/javascript"
             src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
            </script>';
          }
          else{
            echo '<script type="text/javascript"><!--
            google_ad_client = "ca-pub-3655588719499040";
            /* Buzzr Whitelabel Network 234 */
            google_ad_slot = "3972387839";
            google_ad_width = 234;
            google_ad_height = 60;
            //-->
            </script>
            <script type="text/javascript"
            src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
            </script>';
          } 
         echo '</div>';
         }
       }
       /* Ads Unit */
       ?>
       
       <!-- Deal Unit --> 
       <div class="unit_<?php echo $uniqid; ?>">
        <?php 
          $deal_name = $value['offer_name']; //replace with api owners name
          //$deal_link = $value['offer_link']."?utm_source=whitelabel_offers&utm_medium=whitelabel&utm_campaign=$client_name";
		  $deal_link = $value['offer_red_link'];
		  if(empty($deal_link)){
			$deal_link = $value['offer_link'];
		  }
          //$cat_link = $value['offer_categories_url']."?utm_source=whitelabel_offers&utm_medium=whitelabel&utm_campaign=$client_name";
		  $cat_link = $value['offer_categories_url'];
        ?>
        
        <?php
          /* Deal Image */
          if(in_array('image',$fields)){
            $image = $value['offer_image_url'];
            if(empty($image)){
              $image = $value['offer_other_image'];
            }
            if(empty($image)){
              $image = $value['offer_brand_image'];
            }
        ?>
        <div class="image_<?php echo $uniqid; ?>"><a target="_blank" href="<?php echo $deal_link; ?>"><img src="<?php echo $image; ?>" /></a></div>
        <?php } /* Deal Image */ ?>
        
        <?php
          /* Deal Title */
          if(in_array('title',$fields)){
            $deal_name = $value['offer_name'];
        ?>
        <div class="title_<?php echo $uniqid; ?>"><a target="_blank" href="<?php echo $deal_link; ?>"><?php echo $deal_name; ?></a></div>
        <?php } ?>
        
        <?php
          /* Deal Cat */
          if(in_array('category',$fields)){
            $deal_cat_name = $value['offer_categories'];
        ?>
        <div class="cat_<?php echo $uniqid; ?>"><a target="_blank" href="<?php echo $cat_link; ?>"><?php echo $deal_cat_name; ?></a></div>
        <?php } /* Deal Cat */ ?>
        
        <?php
          /* Deal End */
          if(in_array('ends',$fields)){
            $deal_end = $value['offer_end_date'];
        ?>
        <span id="expire_<?php echo $key; ?>" class="expire_<?php echo $uniqid; ?>"></span>
        <script type="text/javascript">
         var expiry = new cdtime("expire_<?php echo $key; ?>", "<?php echo date('F j, Y, G:i:s',strtotime($deal_end)); ?>");
         expiry.displaycountdown("days", formatresults2);  
        </script>
      <?php } /* Deal End */ ?>
      
     </div>
     <!-- Deal Unit -->
     <div style="clear: both;"></div>
     <?php } ?>
     </div>
     <!-- Deal Units Wrap -->
     <?php 
     }
     else{
     ?>
     <div class="unit_wrapper_<?php echo $uniqid; ?>" style="min-height:180px;">
      <div class="unit_<?php echo $uniqid; ?> no_offer_<?php echo $uniqid; ?>">Sorry no offers active</div>
     </div>
     <?php } /* Deals Loop */ ?>
     
     
     
     <?php
      /* City Dropdown */
      if(in_array('city',$fields)){
        $o.= '<div class="cat_list_'.$uniqid.'"><select name="city_select" onchange="window.location.href = this.value;">';
        $o.= '<option value="#">-- Choose City --</option>';
        $city_pod = new Pod('city');
        $city_pod->page = 1;
        $city_pod->search = false;
        $city_pod->findRecords('name ASC', -1);
        while($city_pod->fetchRecord()){
           $citname = $city_pod->get_field('name');
           if(!empty($url_category)){
            $citlink = 'http://buzzr.in/wordpress/buzzr_whitelabel.php?p='.$param.'&c='.$url_category.'&l='.$citname;
           }
           else{
            $citlink = 'http://buzzr.in/wordpress/buzzr_whitelabel.php?p='.$param.'&l='.$citname;
           }
           
           /* Checks for the Selected City to show as Selected in DropDown else populates other options */
           if($citname == $url_city){
            $o.= '<option selected="selected" value="'.$citlink.'">'.$citname.'</option>';
           }
           else{
            $o.= '<option value="'.$citlink.'">'.$citname.'</option>';
           }
           /* Checks for the Selected City to show as Selected in DropDown */
        }
    	  $o.= '</select></div>';
    	  echo $o;
      } 
     ?>
     
     <?php
      /* Category Listing */
      if(in_array('category',$fields)){
     ?>
      <!-- Category Listing -->
     <div class="cat_list_<?php echo $uniqid; ?>">
     <?php
      $category_pod = new Pod('category');
      $category_pod->page = 1;
      $category_pod->search = false;
      $category_pod->findRecords('name ASC', -1,"is_local = 1");
      while($category_pod->fetchRecord()){
         $cname = $category_pod->get_field('name');
         if(!empty($url_city)){
          $clink = 'http://buzzr.in/wordpress/buzzr_whitelabel.php?p='.$param.'&l='.$url_city.'&c='.sanitize_title($cname);
         }
         else{
          $clink = 'http://buzzr.in/wordpress/buzzr_whitelabel.php?p='.$param.'&c='.sanitize_title($cname);
         }
         
         if(sanitize_title($url_category) == sanitize_title($cname)){
          echo '<a style="color:red;" href="'.$clink.'">'.$cname.'</a><br />';
         }
         else{
          echo '<a href="'.$clink.'">'.$cname.'</a><br />';
         }
      }
     ?>
     </div>
     <!-- Category Listing -->
     <?php } ?>
     
     <?php
    /* Ads Sidebar */
    if(in_array('ads',$fields) && count($json_decoded) != 0){
    $ads_sidebar_width = 0.36 * (str_replace('px','',$wrap_width));
    if($ads_sidebar_width >= 250){
    ?>
    <div class="ads_sidebar_<?php echo $uniqid; ?>">
      <script type="text/javascript"><!--
        google_ad_client = "ca-pub-3655588719499040";
        /* Buzzr Whitelabel Network 250 */
        google_ad_slot = "7627060840";
        google_ad_width = 250;
        google_ad_height = 250;
        //-->
        </script>
        <script type="text/javascript"
        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
      </script>
    </div>
    <?php 
    }
    else{
    ?>
    <div class="ads_sidebar_<?php echo $uniqid; ?>">
    <script type="text/javascript"><!--
      google_ad_client = "ca-pub-3655588719499040";
      /* Buzzr Whitelabel Network 200 */
      google_ad_slot = "0360390834";
      google_ad_width = 200;
      google_ad_height = 200;
      //-->
      </script>
      <script type="text/javascript"
      src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script>
    </div>
    <?php
      }
    } /* Ads Sidebar */ ?>
   
     <div style="clear:both;"></div>
    
    <!-- Paginate -->
    <?php
        if(count($json_decoded) != 0){
         /* Next Page Link */
         $next_page_link = 'http://buzzr.in/wordpress/buzzr_whitelabel.php?p='.$param.'&pg='.($url_page + 1);
         
         if(!empty($url_city)){
          $next_page_link = $next_page_link.'&l='.$url_city;
         }
         
         if(!empty($url_category)){
          $next_page_link = $next_page_link.'&c='.$url_category;
         }
         /* Next Page Link */
       } 
       /* Prev Page Link */
       if($url_page > 1){
        $prev_page_link = 'http://buzzr.in/wordpress/buzzr_whitelabel.php?p='.$param.'&pg='.($url_page - 1);
        
        if(!empty($url_city)){
          $prev_page_link = $prev_page_link.'&l='.$url_city;
        }
       
        if(!empty($url_category)){
          $prev_page_link = $prev_page_link.'&c='.$url_category;
        }
       }
       /* Prev Page Link */
    ?>
    <div class="paginate_<?php echo $uniqid; ?>"><?php if(!empty($prev_page_link)){ ?><a href="<?php echo $prev_page_link; ?>">&lt;&lt; Prev Offers</a> | <?php } if(count($json_decoded) != 0){ ?><a href="<?php echo $next_page_link; ?>">More Offers &gt;&gt;</a><?php } ?></div>
    <!-- Paginate -->
    
    <?php
    /* Powered by */
    if(in_array('poweredby',$fields)){
    unset($powered_link);
    //$powered_link = get_bloginfo('url')."?utm_source=whitelabel_offers&utm_medium=whitelabel&utm_campaign=$client_name";
	$powered_link = get_bloginfo('url')."?utm_source=API&utm_medium=API&utm_campaign=$client_name";
    ?>
    <div class="powered_<?php echo $uniqid; ?>"><a target="_blank" href="<?php echo $powered_link; ?>"><img src="<?php echo get_bloginfo('wpurl').'/wp-content/pfiles/images/powered_by_buzzr.jpg'; ?>" /></a></div>
    <?php } /* Powered by */ ?>
    
</div>
<!-- Whitelabel Content Wrap -->

<?php
}
/* If Api Key is Valid Then Proceed */

/* If Api Key is NotValid Then Error */
else{
  echo 'Sorry there seems to be some trouble with your api key, please contact us at <a href="mailto:info@enthrallmedia.com">mailto:info@enthrallmedia.com</a>';
}
/* If Api Key is NotValid Then Error */
?>